/* **********************************************************************************
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* This source code is subject to terms and conditions of the Shared Source License
* for DSL Editor PowerToy. A copy of the license can be found in the License.htm file
* at the root of this distribution. If you can not locate the Shared Source License
* for DSL Editor PowerToy, please obtain a copy from: http://www.codeplex.com/dsltreegrideditor/Project/License.aspx.
* By using this source code in any fashion, you are agreeing to be bound by
* the terms of the Shared Source License for DSL Editor PowerToy.
*
* You must not remove this notice, or any other, from this software.
*
* **********************************************************************************/
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

using EnvDTE;
using EnvDTE80;

using Microsoft.Practices.RecipeFramework;

using DslEditorPowerToy.VisualStudio;

namespace DslEditorPowerToy.Recipes.Actions
{
    /// <summary>
    /// Registers/Unregisters a Visual Studio Package
    /// </summary>
    public class RegisterVsPackageProjectAction : ActionBase
    {
        private Project _packageProject = null;
        private RegisterAction _action = RegisterAction.Register;

        #region Constructors
        public RegisterVsPackageProjectAction()
            : base() 
        { 
        }
        #endregion

        #region Public Properties
        [Input(Required=true)]
        [CLSCompliant(false)]
        public Project PackageProject
        {
            get
            {
                return this._packageProject;
            }
            set
            {
                this._packageProject = value;
            }
        }
        [Input(Required=false)]
        public RegisterAction Action
        {
            get
            {
                return this._action;
            }
            set
            {
                this._action = value;
            }
        }
        #endregion

        #region IAction Members

        public override void Execute()
        {
            base.Execute();
            ValidateParameters();

            //Get the prjects main target output file path
            string targetOutput = DteHelper.GetProjectTargetPath(this._packageProject);
            if (null == targetOutput)
                return;

            //Ensure it exists
            if (false == File.Exists(targetOutput))
                return;

            //Register package in both VS hives
            switch (this._action)
            {
                case RegisterAction.Register:
                    ToolsHelper.RegisterPackage(targetOutput, 
                        (RegistryHive.Standard | RegistryHive.Experimental));
                    break;

                case RegisterAction.Unregister:
                    ToolsHelper.UnregisterPackage(targetOutput,
                        (RegistryHive.Standard | RegistryHive.Experimental));
                    break;
            }
        }
        public override void Undo()
        {
            //Do undo action, or explain why you can't or should not
        }

        #endregion

        #region Protected Methods
        /// <summary>
        /// Validates parameters before execution
        /// </summary>
        protected override void ValidateParameters()
        {
            if (null == this._packageProject)
                throw new ArgumentNullException("PackageProject");
        }
        #endregion

        #region Private Methods
        #endregion
    }

    public enum RegisterAction
    {
        Register,
        Unregister
    }
}
